﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainClient
{
    public partial class frmStaffManager : Form
    {
        public frmStaffManager()
        {
            InitializeComponent();
        }

        // 加载所有用户信息
        private void frmStaffManager_Load(object sender, EventArgs e)
        {
            try
            {

                // Clear all tables
                while (this.dgv_staff.Rows.Count > 0)
                {
                    this.dgv_staff.Rows.RemoveAt(0);
                }

                DataAccess.UserDAO userDAO = new DataAccess.UserDAO();
                DataSet ds = userDAO.getAllUser();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int index = this.dgv_staff.Rows.Add();
                    DataGridViewRow newrow = this.dgv_staff.Rows[index];
                    newrow.Cells[0].Value = dr["登录ID"];
                    newrow.Cells[1].Value = dr["用户名"];
                    newrow.Cells[2].Value = DataAccess.UserDAO.convertToStrDuty(
                                                   Int32.Parse(dr["职位"].ToString()));
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1);
                String strError = "加载员工信息失败！";
                if (MessageBox.Show(strError, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
                {
                    this.Close();
                }
            }
        }

        // 弹出新窗口，显示增加窗体
        private void btn_addStaff_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_staff.SelectedRows[0];
            string strStaffID = selectedRow.Cells[0].Value.ToString().Trim();
            frmStaffInfo frmD = new frmStaffInfo(strStaffID, frmStaffInfo.OPER_ADDSTAFF);
            if (frmD.ShowDialog() == DialogResult.OK)
            {
                this.frmStaffManager_Load(sender, e);
            }
        }

        // 弹出新窗口，显示要移除的窗体
        private void btn_rmStaff_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_staff.SelectedRows[0];
            string strStaffID = selectedRow.Cells[0].Value.ToString().Trim();
            frmStaffInfo frmD = new frmStaffInfo(strStaffID, frmStaffInfo.OPER_RMSTAFF);
            if (frmD.ShowDialog() == DialogResult.OK)
            {
                this.frmStaffManager_Load(sender, e);
            }
        }

        // 弹出新窗口，更新员工信息
        private void btn_updateStaff_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_staff.SelectedRows[0];
            string strStaffID = selectedRow.Cells[0].Value.ToString().Trim();
            frmStaffInfo frmD = new frmStaffInfo(strStaffID, frmStaffInfo.OPER_UPDSTAFF);
            if (frmD.ShowDialog() == DialogResult.OK)
            {
                this.frmStaffManager_Load(sender, e);
            }
        }

        // 修改该用户权限
        private void btn_editPermission_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_staff.SelectedRows[0];
            string strStaffID = selectedRow.Cells[0].Value.ToString().Trim();
            frmEditPermission frmD = new frmEditPermission(strStaffID);
            frmD.ShowDialog();
        }

        private void btn_updPwd_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = this.dgv_staff.SelectedRows[0];
            string strStaffID = selectedRow.Cells[0].Value.ToString().Trim();
            frmUpdPwd frmD = new frmUpdPwd(strStaffID);
            frmD.ShowDialog();
        }
    }
}
